package com.ham.mud.commands;

import com.ham.mud.ServerConnection;
import com.ham.mud.Targetable;
import com.ham.mud.TargetableService;
import com.ham.mud.characters.mobs.Mob;
import com.ham.mud.characters.player.Player;
import com.ham.mud.items.Item;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hlucas on Jun 16, 2011 at 12:45:33 PM
 */
public class LookCommand extends PlayerCommand {
    @Override
    protected String getCommandName() {
        return "look";
    }

    @Override
    public void execute(ServerConnection connection, String[] params) {
        Player player = connection.getPlayer();
        Room room = RoomService.getRoom(player.getZone(), player.getAtX(), player.getAtY());

        if(params == null || params.length == 0) {
            room.display(connection);
        } else {
            if(params[0].equals("self")) {
                player.display(connection);
                return;
            }

            List<ServerConnection> others = room.getOtherConnections(connection);
            List<Player> players = new ArrayList<Player>();
            for(ServerConnection other : others) {
                players.add(other.getPlayer());
            }

            Targetable targetable = TargetableService.findTarget(params[0], room.getMobs(), room.getItems(), players);
            if(targetable == null) {
                connection.printSolo("There's nothing by that name here.");
            } else {
                if(targetable instanceof Player) {
                    ((Player)targetable).display(connection);
                } else if(targetable instanceof Mob) {
                    ((Mob)targetable).display(connection);
                } else if(targetable instanceof Item) {
                    ((Item)targetable).display(connection);
                }
            }
        }

    }
}
